<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">

          <a-col :span="6">
            <a-form-item :label="$t('columns.imei')">
              <a-input :placeholder="$t('placeholder.imei', {label: $t('placeholder.imei')})" v-model="queryParam.imei"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
<!--    <div class="table-operator">
&lt;!&ndash;      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>&ndash;&gt;
      <a-button type="primary" icon="download" @click="handleExportXls('终端上线临时')">导出</a-button>
&lt;!&ndash;      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>&ndash;&gt;
      &lt;!&ndash; 高级查询区域 &ndash;&gt;
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>{{ $t('common.table.delete') }}</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>-->

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> {{ $t('btns.selected') }} <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>{{ $t('pagination.items') }}
        <a style="margin-left: 24px" @click="onClearSelected">{{ $t('btns.clear') }}</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="syncIot(record)">{{ $t('btns.sync') }}</a>

<!--          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>-->
        </span>

      </a-table>
    </div>

    <iot-terminal-temp-modal ref="modalForm" @ok="modalFormOk"></iot-terminal-temp-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import IotTerminalTempModal from './modules/IotTerminalTempModal'
  import { getAction, postAction } from '@/api/manage'
  export default {
    name: 'IotTerminalTempList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      IotTerminalTempModal
    },
    data () {
      return {
        description: this.$t('title'),
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:this.$t('columns.imei'),
            align:"center",
            dataIndex: 'imei'
          },
          {
            title:this.$t('columns.sn'),
            align:"center",
            dataIndex: 'sn'
          },
          {
            title:this.$t('columns.addTime'),
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title: this.$t('common.table.actions'),
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/rental/iotTerminalTemp/list",
          delete: "/rental/iotTerminalTemp/delete",
          deleteBatch: "/rental/iotTerminalTemp/deleteBatch",
          exportXlsUrl: "/rental/iotTerminalTemp/exportXls",
          importExcelUrl: "rental/iotTerminalTemp/importExcel",
          syncIot:"/rental/iotTerminalTemp/syncIot",
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'imei',text:'imei编号',dictCode:''})
        fieldList.push({type:'string',value:'sn',text:'sn号',dictCode:''})
        fieldList.push({type:'string',value:'createTime',text:'上报时间',dictCode:''})
        this.superFieldList = fieldList
      },
      syncIot(record){
        console.log("syncIot 方法被调用", record);
        debugger;
        this.loading = true; // 开始加载状态
        // 构建请求数据
        const params = {
          imei: record.imei,
          sn: record.sn,
          id:record.id
          // 添加其他需要同步的字段
        };
        // 发送同步请求
        postAction(this.url.syncIot, params)
          .then(res => {
            if (res.success) {
              this.$message.success('同步成功');
              this.loadData(); // 重新加载数据
            } else {
              this.$message.error('同步失败: ' + res.message);
            }
          })
          .catch(error => {
            this.$message.error('同步错误: ' + error.message);
          })
          .finally(() => {
            this.loading = false; // 结束加载状态
          });
      }
    }
  }
</script>

<i18n>
{
  "zh-CN": {
    "title": "终端上线临时管理",
    "columns": {
      "imei": "imei编号",
      "sn": "sn号",
      "addTime": "添加时间"
    },
    "btns": {
      "export": "导出",
      "advancedQuery": "高级查询",
      "selected": "已选择",
      "clear": "清空",
      "sync": "同步"
    },
    "placeholder": {
      "imei": "请输入imei号"
    },
    "pagination": {
      "total": "共",
      "items": "条"
    }
  },
  "en-US": {
    "title": "Terminal Online Temporary Management",
    "columns": {
      "imei": "IMEI Number",
      "sn": "SN Number",
      "addTime": "Add Time"
    },
    "btns": {
      "export": "Export",
      "advancedQuery": "Advanced Query",
      "selected": "Selected",
      "clear": "Clear",
      "sync": "Sync"
    },
    "placeholder": {
      "imei": "Please enter imei"
    },
    "pagination": {
      "total": "Total",
      "items": "items"
    }
  },
  "fr-FR": {
    "title": "Gestion temporaire des terminaux en ligne",
    "columns": {
      "imei": "Numéro IMEI",
      "sn": "Numéro SN",
      "addTime": "Temps d'ajout"
    },
    "btns": {
      "export": "Exporter",
      "advancedQuery": "Requête avancée",
      "selected": "Sélectionné",
      "clear": "Effacer",
      "sync": "Synchroniser"
    },
    "placeholder": {
      "imei": "Veuillez saisir imei"
    },
    "pagination": {
      "total": "Total",
      "items": "éléments"
    }
  }
}
</i18n>
<style scoped>
  @import '~@assets/less/common.less';
</style>